In this lab exercise, we make use of user-defined functions in order to display the actual size of an image while fitting it to the screen boundaries. We provide two functions: one for displaying a single image and the other for displaying two images side-by-side. Here are the code and sample usages of these functions are presented below.
def get_figsize(im):
# What size does the figure need to be in inches to fit the image?
dpi = plt.rcParams['figure.dpi']
dim = im.shape
figuresize = dim[1]/float(dpi), dim[0]/float(dpi)
return(figuresize)
def display_image_actual_size_single(im_data):
figuresize = get_figsize(im_data)
# Create a figure of the right size with one axes that takes up the full figure
fig = plt.figure(figsize=figuresize)
# Add the single axis to fit the image to the screen boundary
ax = fig.add_axes([0, 0, 1, 1])
ax.imshow(im_data, cmap='gray')
ax.axis('off')
plt.show()
return(fig)
def display_image_actual_size_double(im_data1,im_data2):
# assuming that the two input images have the same dimension and shape
# then, we calculate figsize from one of the two images
figuresize = get_figsize(im_data1)
# Create a figure of the right size that can accommodate two images side-by-side
fig = plt.figure(figsize=(figuresize[0],figuresize[1]*2+.1))
ax1 = fig.add_axes([0,0,1,1])
ax1.imshow(im_data1, cmap='gray')
ax1.axis('off')
ax2 = fig.add_axes([1.1,0,1,1])
ax2.imshow(im_data2, cmap='gray')
ax2.axis('off')
return(fig)
First, let us look at our background and foreground images that we have chosen. Load them into your Python enviroment and display them. The code here illustrates how to use both of our user-defined image-display functions. Also, we show you two foreground cases (as noted above); you need only one.
fig = display_image_actual_size_single(bg)
fig = display_image_actual_size_double(fgD, fgB)
Decide where on the background you want to put your foreground image. Cut from the background this region of interest (roi); it is of the same width, same height, and same color channels as the foreground image. For example, in our case here, we will put the logo at the bottom right of our poster. The logo's top-left corner will be at the coordinate (row=390, column=765) of the poster. Remember image axes? Row 0 is at the top.
rows, cols, channels = fg.shape #get the width, the height, and the channels of fg
roi = bg[390:390+rows, 765:765+cols] #cut from bg the roi of the same dimesions as fg
Following the lecture where we create a black-and-white image of a robot, we first convert the foreground image to grayscale, named it fgGrayscale. Then, we threshold it using OpenCV threshold function (provided below). It returns two outputs, the thresholded image is the latter. The second variable thres is your threshold.
retval, mask = cv2.threshold(fgGrayscale, thres, 255, cv2.THRESH_BINARY)
The next steps involves applying one or more bitwise operations, making use of the binary mask we have just created. We give you the expected output; it is your task to figure out the process. Below is an OpenCV command for 'and' operation. The syntax of other bitwise operations are the same (consult OpenCV library to learn more).
result = cv2.bitwise_and(img, mask)